//
//  NSString+Date.m
//  Community
//
//  Created by xujia on 16/6/21.
//  Copyright © 2016年 MaryKayChina. All rights reserved.
//

#import "NSString+Date.h"

@implementation NSString (Date)

- (NSDate*)getDateFromNet
{
	NSString *number = [self getData:self withFormat:@"\\b(\\d*)\\b"];
	if (number) {
		double createdTime = [number doubleValue];
		return [NSDate dateWithTimeIntervalSince1970:(createdTime/1000)];
	}else{
		double createdTime = [self doubleValue];
		return [NSDate dateWithTimeIntervalSince1970:createdTime];
	}
}

- (NSString*)getData:(NSString*)data withFormat:(NSString*)format
{
	NSString *str = nil;
	NSError *error = nil;
	NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:format options:NSRegularExpressionCaseInsensitive error:&error];
	NSArray *matches = [regex matchesInString:data options:0 range:NSMakeRange(0, [data length])];
	//NSLog(@"match count: %d", [matches count]);
	for (NSTextCheckingResult *result in matches) {
		//NSLog(@"match:%@", [data substringWithRange:result.range]);
		str = [data substringWithRange:result.range];
		if ([str length] > 0) {
			return str;
		}
	}
	if ([matches count] == 0) {
		return nil;
	}
	else
		return str;
}

+ (NSString *)convertFromSecondToDateString:(NSInteger)second {
    NSInteger durationN = second;
    NSInteger hourN = durationN / DATE_HOUR;
    NSInteger minuteN = durationN / DATE_MINUTE - hourN * DATE_MINUTE;
    NSInteger secondN = durationN - hourN * DATE_HOUR - minuteN * DATE_MINUTE;
    
    NSString *dateString = @"";
    if (hourN > 0) {
        dateString = [NSString stringWithFormat:@"%ld小时%ld分%ld秒",(long)hourN ,(long)minuteN ,(long)secondN];
    } else if (hourN <= 0 && minuteN > 0) {
        dateString = [NSString stringWithFormat:@"%ld分%ld秒",(long)minuteN,(long)secondN];
    } else {
        dateString = [NSString stringWithFormat:@"%ld秒",(long)secondN];
    }
    return dateString;
}

@end
